<?php
session_start();
include "../include/dbFunctions.php";
include "../include/formFunctions.php";
$vid = $_GET['id'];
$eid = $_SESSION['eid'];
$queryt= "SELECT * FROM session WHERE event_id = $eid";
$session_array = executeSelectQuery($queryt);
//session detail
//first-last date of event
$first = $last = $session_array[0]['session_date'];
for ($i=0;$i<count($session_array);$i++) {
    if ($session_array[$i]['session_date']<$first) {
        $first = $session_array[$i]['session_date'];
    }
    if ($session_array[$i]['session_date']>$last) {
        $last = $session_array[$i]['session_date'];
    }
}
$querys= "SELECT *
            FROM school s, event e, location l, education ed
            WHERE s.education_id = ed.education_id
            AND e.school_id = s.school_id
            AND s.location_id = l.location_id
            AND e.event_id =$eid";
$school_array = executeSelectQuery($querys);
//School detail
$education_id = $school_array[0]['education_id'];
$location_id  = $school_array[0]['location_id'];
//
//
//
//
//
//
//
$i = 0;

//
$querysp= "SELECT * FROM time_preference p, time t WHERE t.time_id = p.time_id AND volunteer_id = $vid";
$session_preference = executeSelectQuery($querysp);
//
$queryv = "SELECT * FROM volunteer WHERE volunteer_status ='active' AND volunteer_id=$vid";
$volunteer_array = executeSelectQuery($queryv);
//
$volunteer_id = $volunteer_array[$i]['volunteer_id'];
//
//
//Only for detail
$volunteer_array[$i]['sessions']= $volunteer_array[$i]['edus']=$volunteer_array[$i]['locations']=$volunteer_array[$i]['ongoings']=0;
$query11 = "SELECT DISTINCT(education_name) FROM education_preference, education WHERE volunteer_id= $vid";
$edu_pre = executeSelectQuery($query11);
$query12 = "SELECT DISTINCT(location_name) FROM location_preference, location WHERE volunteer_id= $vid";
$loc_pre = executeSelectQuery($query12);
//match1: session match
$query20 = "SELECT * FROM session WHERE volunteer_id= $vid";
$ongoing_session = executeSelectQuery($query20);
$weekday = Array('Monday','Tuesday','Wednesday','Thursday','Friday');
$result=0;
for ($y=0;$y<count($session_array);$y++) {
    $match = false;
    $session_array[$y]['m']="not";
    $session_array[$y]['ce']="no";
    $session_array[$y]['dw']="Weekend";
    //get weekday/weekend
    $date = $session_array[$y]['session_date'];
    $session_date = explode('-',$date);
    $day = $session_date[2];
    $month = $session_date[1];
    $year = $session_date[0];
    $session_array[$y]['day']  = date("l", mktime(0,0,0,$month,$day,$year));
    //check if it's weekday/weekend
    $day_id = 1;
    for ($j=0;$j<count($weekday);$j++) {
        if ($weekday[$j]==$session_array[$y]['day']) {
            $day_id = 0;
            $session_array[$y]['dw']="Weekday";
        }
    }
    //check if it's match
    for ($l=0;$l<count($session_preference);$l++) {
        if ($day_id==$session_preference[$l]['time_id']) {
            //now check if the match is on current event
            $match = true;
        }
        if ($match==true) {
            for ($k=0;$k<count($ongoing_session);$k++) {
                if ($date==$ongoing_session[$k]['session_date']) {
                    $session_array[$y]['ce']="yes";
                    $match = false;
                }
            }
        }
    }
    $session_array[$y]['match'] = $match;
    if ($session_array[$y]['match']==true) {
        $result += 69/(count($session_array));
        $session_array[$y]['m']="match";
        $volunteer_array[$i]['sessions'] = $result;
    }

}
if ($result==69) {
    $volunteer_array[$i]['session']="match";
} elseif ($result==0) {
    $volunteer_array[$i]['session']="not";
} else {
    $volunteer_array[$i]['session']="partial";
}
$volunteer_array[$i]['match']=$result;
//match2: educational preference
$query2= "SELECT * FROM education_preference
                    WHERE education_id = $education_id
                    AND volunteer_id = $vid";
$result2 = executeSelectQuery($query2);
if ($result2 != null) {
    $volunteer_array[$i]['education']= "match";
    $volunteer_array[$i]['match'] += 10;
    $volunteer_array[$i]['edus'] = 10;
} else {
    $volunteer_array[$i]['education']="not";
}
//match3: location
$query3= "SELECT * FROM location_preference
                    WHERE location_id = $location_id
                    AND volunteer_id = $vid";
$result3 = executeSelectQuery($query3);
if ($result3 != null) {
    $volunteer_array[$i]['location']= "match";
    $volunteer_array[$i]['match'] += 10;
    $volunteer_array[$i]['locations'] = 10;
} else {
    $volunteer_array[$i]['location']="not";
}
//match 4: ongoing\ REDOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
$queryoe = "SELECT *
                    FROM event e, trainer t
                    WHERE e.event_id = t.event_id
                    AND t.volunteer_id = $vid
                    AND t.trainer_status= 'confirmed'
                    AND e.event_id != $eid";
$ongoing_event= executeSelectQuery($queryoe);
$volunteer_array[$i]['ongoing']="match";
if ($ongoing_event!=null) {
    for ($k=0;$k<count($ongoing_event);$k++) {
        $query51 = "SELECT * FROM session WHERE event_id = ".$ongoing_event[$k]['event_id'];
        $session_ongoing = executeSelectQuery($query51);
        $ongoing_event[$k]['first'] = $ongoing_event[$k]['last'] = $session_ongoing[0]['session_date'];
        for ($m=0;$m<count($session_ongoing);$m++) {
            if ($ongoing_event[$k]['first']>$session_ongoing[$m]['session_date']) {
                $ongoing_event[$k]['first'] = $session_ongoing[$m]['session_date'];
            }
            if ($ongoing_event[$k]['last']<$session_ongoing[$m]['session_date']) {
                $ongoing_event[$k]['last'] = $session_ongoing[$m]['session_date'];
            }
        }
        $ongoing_event[$k]['dup']=0;
        
    }
    for ($k=0;$k<count($ongoing_event);$k++) {
        if (((($first<$ongoing_event[$k]['last'])&&($first>$ongoing_event[$k]['first']) ))||(
                (($last<$ongoing_event[$k]['last'])&&($last>$ongoing_event[$k]['first']))) ) {
            $ongoing_event[$k]['dup']=1;
            $volunteer_array[$i]['ongoing']="not";
        }
    }
}
if ($volunteer_array[$i]['ongoing']=="match") {
    $volunteer_array[$i]['match'] += 10;
    $volunteer_array[$i]['ongoings'] = 10;
}
?>
<?php
include "../include/menu.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>JA Scheduling</title>
        <script type="text/javascript" language="javascript" src="../media/js/jquery/jquery-1.6.1.min.js"></script>
        <!--  CSS script -->
        <link rel="stylesheet" href="../media/css/screen.css" type="text/css" media="screen" title="default" />
    </head>
    <body>
        <!-- DON'T TOUCH! Start: page-top-outer -->
        <?php
        top_outer();
        ?>
        <!-- End: page-top-outer -->

        <div class="clear">&nbsp;</div>

        <!--  start nav-outer-repeat................................................................................................. START -->
        <div class="nav-outer-repeat">
            <!--  start nav-outer -->
            <div class="nav-outer">
                <!-- start nav-right -->
                <?php
                nav_right();
                ?>
                <!-- end nav-right -->
                <!--  start nav -->
                <div class="nav">
                    <div class="table">
                        <?php
                        navigation_event();
                        ?>
                        <div class="clear"></div>
                    </div>
                    <div class="clear"></div>
                </div>
                <!--  start nav -->
            </div>
            <div class="clear"></div>
            <!--  start nav-outer -->
        </div>
        <!--  start nav-outer-repeat................................................... END -->
        <div class="clear"></div>
        <!-- start content-outer ........................................................................................................................START -->
        <div id="content-outer">
            <!-- start content -->
            <div id="content">
                <!--  start page-heading --> <!--lyedit-->
                <div id="page-heading">
                    <h1>Volunteer Match Detail <font color="red">( <?php echo $volunteer_array[$i]['match']?>% )</font></h1>
                </div>
                <!-- end page-heading -->

                <table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
                    <tr>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
                        <th class="topleft"></th>
                        <td id="tbl-border-top">&nbsp;</td>
                        <th class="topright"></th>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
                    </tr>
                    <tr>
                        <td id="tbl-border-left"></td>
                        <td>
                            <!--  start content-table-inner ...................................................................... START -->
                            <div id="content-table-inner">
                                <!--  start table-content  -->
                                <div id="table-content">
                                    <!--  Can put table start from here within this div--> <!--lyedit-->
                                    <p><b>Volunteer's name:</b> <?php echo "<a href='../volunteer/volunteerView.php?id=".$vid."'>".$volunteer_array[$i]['volunteer_firstname']." ".$volunteer_array[$i]['volunteer_surname']."</a>";?></p><br/>
                                    <h3>Match 1: Session Date <font color="red">(<?php echo $volunteer_array[$i]['sessions']?>/69)</font></h3>
                                    <table border="1" width="100%">
                                        <tr>
                                            <th class="thmain">Volunteer Preference</th>
                                            <th class="thmain">Match with weekday preference</th>
                                        </tr>
                                        <tr>
                                            <td class="tdmain">
                                                <?php
                                                for ($i=0;$i<count($session_preference);$i++) {
                                                    echo "- ".$session_preference[$i]['time_name']."<br/>";
                                                }
                                                ?>
                                            </td>
                                            <td class="tdmain">
                                                <table border="0" width ="100%"height="100%">
                                                    <th class="thmain">Session Date</th>
                                                    <th class="thmain">Day</th>
                                                    <th class="thmain">Weekday/Weekend</th>
                                                    <th class="thmain">In current event</th>
                                                    <th class="thmain">Match</th>
                                                    <?php
                                                    for ($i=0;$i<count($session_array);$i++) {
                                                        echo "<tr>";
                                                        echo "<td class=tdmain'>".$session_array[$i]['session_date']."</td>";
                                                        echo "<td class=tdmain'>".$session_array[$i]['day']."</td>";
                                                        echo "<td class=tdmain'>".$session_array[$i]['dw']."</td>";
                                                        echo "<td class=tdmain'>".$session_array[$i]['ce']."</td>";
                                                        echo "<td class=tdmain'>".icon($session_array[$i]['m'])."</td>";
                                                        echo "</tr>";
                                                    }
                                                    ?>
                                                </table>
                                            </td>
                                        </tr>
                                    </table>
                                    <br/>
                                    <h3>Match 2: Educational Level <font color="red">(<?php $i=0;
                                            echo $volunteer_array[$i]['edus']; ?>/10)</font></h3>
                                    <table border="1" width="100%" >
                                        <tr>
                                            <th class='thmain'>Volunteer Preference</th>
                                            <th class='thmain'>Match with educational level preference</th>
                                        </tr>
                                        <tr>
                                            <td class='tdmain'>
                                                <?php
                                                for ($i=0;$i<count($edu_pre);$i++) {
                                                    echo "- ".$edu_pre[$i]['education_name']."<br/>";
                                                }
                                                ?>
                                            </td>
                                            <td class='tdmain'>
                                                <table border="0" width="100%">
                                                    <tr>
                                                        <td class='tdmain'><?php echo $school_array[0]['education_name']; ?></td>
                                                        <td class='tdmain'><?php echo icon($volunteer_array[0]['education']); ?></td>
                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>
                                    </table>
                                    <br/>
                                    <h3>Match 3: Location <font color="red">(<?php $i=0;
                                            echo $volunteer_array[$i]['locations']?>/10)</font></h3>
                                    <table border="1" width="100%" >
                                        <tr>
                                            <th class='thmain'>Volunteer Preference</th>
                                            <th class='thmain'>Match with location preference     </th>
                                        </tr>
                                        <tr>
                                            <td class='tdmain'>
                                                <?php
                                                for ($i=0;$i<count($loc_pre);$i++) {
                                                    echo "- ".$loc_pre[$i]['location_name']."<br/>";
                                                }
                                                ?>
                                            </td>
                                            <td class='tdmain'>
                                                <table border="0" width="100%">
                                                    <tr>
                                                        <td class='tdmain'><?php echo $school_array[0]['location_name']; ?></td>
                                                        <td class='tdmain'><?php echo icon($volunteer_array[0]['location']); ?></td>
                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>
                                    </table>
                                    <br/>
                                    <h3>Match 4: Not in any other ongoing event <font color="red">(<?php $i=0;
                                            echo $volunteer_array[$i]['ongoings']?>/10)</font></h3>
                                    <table border="1" width="100%">
                                        <tr>
                                            <th class='thmain'>Volunteer's Ongoing Event</th>
                                            <th class='thmain'>Event Detail</th>
                                        </tr>
                                        <tr>
                                            <td class='tdmain'>
                                                <table border="0" width="100%">
                                                    <tr>
                                                        <th class='thmain'>Event Name</th>
                                                        <th class='thmain'>Start Date</th>
                                                        <th class='thmain'>End Date</th>
                                                    </tr><tr>
                                                        <?php
                                                        for ($t=0;$t<count($ongoing_event);$t++) {
                                                            if ($ongoing_event[$t]['dup']==true) {
                                                                echo "<tr>";
                                                                echo "<td class='tdmain'>".$ongoing_event[$t]['event_name']."</td>";
                                                                echo "<td class='tdmain'>".$ongoing_event[$t]['first']."</td>";
                                                                echo "<td class='tdmain'>".$ongoing_event[$t]['last']."</td>";
                                                                echo "</tr>";
                                                            }
                                                        }
                                                        ?>
                                                    </tr>
                                                </table>
                                            </td>
                                            <td class='tdmain'>
                                                <table border="0" width="100%">
                                                    <tr>
                                                        <td class='tdmain'>From <?php echo $first?> to <?php echo $last?></td>
                                                        <td class='tdmain'><?php echo icon($volunteer_array[0]['ongoing'])?></td>
                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>
                                    </table>
                                    <br/>
                                    <input type="button" value="" class="form-back" onClick="javascript:history.go(-1)"/>
                                    <!--  end table-content here -->
                                </div>
                                <div class="clear"></div>
                            </div>
                            <!--  end content-table-inner ............................................END  -->
                        </td>
                        <td id="tbl-border-right"></td>
                    </tr>
                    <tr>
                        <th class="sized bottomleft"></th>
                        <td id="tbl-border-bottom">&nbsp;</td>
                        <th class="sized bottomright"></th>
                    </tr>
                </table>
                <div class="clear">&nbsp;</div>

            </div>
            <!--  end content -->
            <div class="clear">&nbsp;</div>
        </div>
        <!--  end content-outer........................................................END -->
        <?php
        footer();
        ?>

    </body>
</html>




